<?php

/**
 * Description of GetHttp
 *
 * @author  赵福堂
 */
class GetHttp {
    
    private $Url;
    private $Host;
    private $Base;
    private $Html;
    private $selector   =   array(
            'link'=>'href',
            'script'=>'src',
            'img'=>'src',
            'a' => 'href'
        );
    
    public function init($url , $filename){    
        if(!Core::isUrl($url)){
            Core::setError("你输入的 URL 不正确！");
            return false;
        }
        if(!$filename){
            Core::setError("你输入的 文件名 不正确！");
            return false;
        }else{
            $filename   =   "{$filename}.html";
        } 

        $this->Url   =   $url;   
        
        $host    =    parse_url($this->Url);
        if(is_array($host)){
            $this->Host    =    $host['scheme'] ."://" . $host['host'] ;
            $this->Base    =    $this->Host . "/";
        }
        $this->_SetDir($host['host']);
        
        $this->Html =   file_get_html($this->Url);
        
        if(!$this->Html){
            Core::setError("URL 内容为空！");
            return false;
        }
        
        $this->_SetBase();#设置base
        $this->_Analyze();#解析与下载
        
        Core::saveData($filename, $this->Html);    #保存        
        
    }   
    
    public function setSelector($key,$value){
        $this->selector[$key]   =   $value;
    }

    
    private function _Analyze(){        
        $selector   =   $this->selector;

        if(is_array($selector)){
            
            foreach($selector as $key=>$val){  
                $downloadFile   =   array();
                foreach($this->Html->find($key) as $e){
                    //参数为空 直接跳过
                    if(empty($e->$val)){
                        continue;
                    }
                    //后续动作a标签不参与
                    if($key =="a"){
                        $e->$val    =   "#";
                        continue;
                    }

                    $fileUrl  =   Core::setFilePath($e->$val , $this->Base);
                    $fileName =  Core::setFileName(Core::$downloadPath[$key] , $e->$val);   
                    //排除url
                    if(Core::notUrl($fileUrl)){
                        continue;
                    }
                    #检查url
                    if(!Core::isUrl($fileUrl)){
                        Core::setError("url 不合法！{$fileUrl} ");
                        continue;
                    }                  
                    $downloadFile[$fileName] =   $fileUrl;
                    $e->$val  =   $fileName;
                }
                //后续动作a标签不参与
                if($key =="a")
                    continue;
                //不同的文件不同的下载方式 
                if(!$hockClass  =   Core::getClass("{$key}Analyz")){
                    $hockClass  =   Core::getClass('DefaultAnalyze');#加载默认
                }                
                if($hockClass){
                    $hockClass->doStart($downloadFile);  
                }
            }
        }
        
        return ;
    }    
   

    private function _SetBase(){
        foreach($this->Html->find('base') as $e){
            
            if(empty($e->href)){
                return ;
            }else if(substr($e->href, -1 , 1) == "/"){
                $this->Base =   $e->href;
            }else{
                $this->Base =   $e->href  . "/";
            }
            //清除base标签
            $e->outertext   =   "";
        }      
    }
    
    
    
    private function _SetDir($host){       
        
        Core::setDownloadPath("base","data".DIRECTORY_SEPARATOR.$host.DIRECTORY_SEPARATOR );#主目录
        Core::setDownloadPath("img" ,  "images".DIRECTORY_SEPARATOR);#图片目录
        Core::setDownloadPath("script" ,  "js".DIRECTORY_SEPARATOR);#js目录
        Core::setDownloadPath("link" ,  "style".DIRECTORY_SEPARATOR);#链接目录
        
        foreach(Core::$downloadPath as $key=>$item){
            if($key != "base")
                $item   =   Core::$downloadPath['base'].$item;
            if(!is_dir($item)){
                if(!mkdir($item)){
                  exit("mkdir is error! \n {$item}");            
                }
              }
        }        
        
    }

}

